package fr.gmdev.mail.fonction.mail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.SMTPMessage;

public class SMTPManager {

	private static Session ms ;
	
	
	public SMTPManager(String smtHost, int smtpPort) {
		super();
		Properties props = new Properties();
		props.put("mail.smtp.host", smtHost);
		props.put("mail.smtp.port", Integer.valueOf(smtpPort).toString());
		props.put("mail.transport.protocol","smtp");
		ms = Session.getInstance(props);
	}

	public SMTPManager() {
		this("172.20.46.91", 10025);
	}
	
	public MimeMessage createMessage(){
		return new MimeMessage(ms);
	}
	
	public void sendMail (MimeMessage message) throws NoSuchProviderException, MessagingException{
		ms.getTransport().send(message);
	}

	
//	SMTPMessage messageAEnvoyer = new SMTPMessage((MimeMessage)msg[i]);
//	messageAEnvoyer.setSubject("msg:"+i+"  " + messageAEnvoyer.getSubject() );
////messageAEnvoyer.setFrom(new InternetAddress("gilbert.marx@jedeclare.com"));
//	if( boite.contains("2")){ // TDFC
//		messageAEnvoyer.setRecipient(RecipientType.TO, new InternetAddress("edi-tdfc@mail-recette.jedeclare.com"));
//	}else if (boite.contains("3")){ // TVA
//		messageAEnvoyer.setRecipient(RecipientType.TO, new InternetAddress("edi-tva@mail-recette.jedeclare.com"));
//	}else if (boite.contains("5")) { // DADSU
//		messageAEnvoyer.setRecipient(RecipientType.TO, new InternetAddress("dadsu@mail-recette.jedeclare.com"));
//	}
//while(nbTry-- >0){
//	try {
//		// envoie
//		ms.getTransport().send(messageAEnvoyer);
//		System.out.println("message envoy� : " + messageAEnvoyer.getAllRecipients()[0]+ "/" + messageAEnvoyer.getSubject());

	

}